import 'package:dynamic_stock/const/const.dart';
import 'package:dynamic_stock/model/home_model.dart';
import 'package:flutter/material.dart';

class CityPage extends StatelessWidget {
  
  final List<DynamicStockList> list;

  CityPage({Key key, @required this.list}) : super(key: key);

  final TextStyle normalStyle = TextStyle(color: Color(0xff5F5D70), fontSize: 14);

  String _getPersent(String val) {
    return (double.parse(val) * 100).toStringAsFixed(2) + "%";
  }

  Widget _createItem(DynamicStockList model, int index){

    // 创建排名标签
    Widget tagWidget = Container(
            height: 25,
            decoration: BoxDecoration(
              color: index == 0 ? Color(0xffFF6903) : Color(0xffFFA303),
              borderRadius: BorderRadius.horizontal(left: Radius.circular(12.5))
            ),
            child: Row(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Image.asset("assets/hat.png"),
                ),
                Text(
                  "No." + model.sort.toString(),
                  style: TextStyle(color: Colors.white, fontSize: 13, fontWeight: FontWeight.w500),
                )
              ],
            ),
          );

    return Container(
      color: index == 0 ? Color(0xffF6FDFA) : Colors.white,
      child: Column(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.only(top: 36,left: 25),
            child: Row(
              children: <Widget>[
                Image.asset('assets/company_icon.png'),
                SizedBox(width: 13.5,),
                // 公司名称
                Expanded(
                  child: Text(
                    model.saasComName, 
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(color: AppMainColor.tabbarActiveColor, fontSize: 17, fontWeight: FontWeight.bold),),
                ),
                tagWidget
              ],
            ),
          ),

          Padding(
            padding: const EdgeInsets.only(top: 21,left: 25),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Text("上架车辆(台)", style: normalStyle,),
                    SizedBox(width: 15,),
                    // 上架车辆
                    Text(model.totalCarCount, style: normalStyle)
                  ],
                ),
                Row(
                  children: <Widget>[
                     Text("占比", style: normalStyle,),
                      SizedBox(width: 15,),
                      // 占比
                      Container(
                        width: 100,
                        child: Text(_getPersent(model.onAppCarCount), style: normalStyle,),
                      )
                 ]
                )
              ],
            ),
          ),

          Padding(
            padding: const EdgeInsets.only(top: 14,left: 25),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Text("消费金额(元)", style: normalStyle,),
                    SizedBox(width: 15,),
                    // 消费金额
                    Text(model.carComsumeAmount, style: normalStyle,)
                  ],
                ),
                Row(
                  children: <Widget>[
                    Text("占比", style: normalStyle,),
                    SizedBox(width: 15,),
                    // 消费金额占比
                    Container(
                      width: 100,
                      child: Text(_getPersent(model.carComsumePercent), style: normalStyle,),
                    )
                  ],
                )
              ],
            ),
          ),

          Container(
            padding: const EdgeInsets.only(top: 19, left: 25, right: 25),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                _createTextTag("链上Hash", Color(0xffFCE9C8), Color(0xffF5A623)),
                SizedBox(width: 29),
                Expanded(
                  // 链上Hash
                  child: Text(
                    model.tHash ?? "--",
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(color: Color(0xffB2B0C0), fontSize: 12),
                  ),
                ),
              ],
            ),
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(25, 17, 25, 30),
            child: Column(
              children: <Widget>[
                _createProgress("贡献度", Color(0xffD7D6DB), Color(0xff5F5D70), Color(0xff5F5D70), model.contributionDegree),
                SizedBox(height: 21.5),
                _createProgress("动态股份", Color(0xffE5FAF5), Color(0xff00CA9D), Color(0xff00CA9D), model.dynamicStock)
              ],
            ),
          ),
          Container(color: AppMainColor.normalBackColor, height: 10,)
        ],
      ),
    );
  }

  Widget _createTextTag(String text, Color backColor, Color color) {
    return Container(
            width: 60,
            height: 20,
            alignment: Alignment.center,
            color: backColor,
            child: Text(text, style: TextStyle(color: color, fontSize: 12),),
          );
  }

  Widget _createProgress(String title, Color textBackColor, Color textColor, Color sliderColor, String val) {
    return Row(
            children: <Widget>[
              _createTextTag(title, textBackColor, textColor),
              SizedBox(width: 25.5,),
              Expanded(
                child: Container(
                  height: 2,
                  child: LinearProgressIndicator(
                    backgroundColor: Color(0xffDFE2E6),
                    valueColor: AlwaysStoppedAnimation(sliderColor),
                    value: double.parse(val),
                  ),
                )
              ),
              SizedBox(width: 23,),
              Container(
                height: 20,
                padding: const EdgeInsets.symmetric(horizontal: 7.5),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black12, 
                      offset: Offset(0,1.5),
                      blurRadius: 4
                    ),
                  ]
                ),
                child: Text(
                  _getPersent(val),
                  style: TextStyle(color: Color(0xff5F5D70), fontSize: 11),
                ),
              )
            ],
          );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        leading: FlatButton(
          child: Image.asset("assets/system-backnew.png"),
          onPressed: (){
            Navigator.of(context).pop();
          },
        ),
        title: Text("城市合伙人公司", style: AppTextStyle.tabbarTitleStyle),
        elevation: 0.5,
      ),
      body: Container(
        color: AppMainColor.normalBackColor,
        child: ListView.builder(
          itemCount: list.length,
          itemBuilder: (context, index){
            return _createItem(list[index],index);
          },
        ),
      )
    );
  }
}